/*
Created by Vadim Shchemelinin aka badger 
2012 St.Petersburg

Not Allow to go out of zone.

[_unit,_zones,_markerSafeZone,_startPos,_condition,_timeout,_safeDistance,_colorMarker] call bgr_fnc_allowZone;

example, not allow to go from "zoneMarker" all game
0 = [player,["zoneMarker"],true,(getPosATL vehicle player),"false",0.5,0,false] call bgr_fnc_allowZone;

*/
_unit = _this select 0;
_zones = _this select 1;
_markerSafeZone = _this select 2;
_startPos = _this select 3;
_cond = compile (_this select 4);
_timeout = _this select 5;
_safeDistance = _this select 6;
_colorMarker = _this select 7;

_myZoneI = -1;
_myZone = "bgr_frizTimeZoneMarker";

if (_markerSafeZone) then {
	waitUntil{!isNil("CBA_fnc_inArea")};	
	_i = 0;
	{
		//select started zone
		_res = [_startPos,_x] call CBA_fnc_inArea;
		if (_res) then {
			_myZoneI = _i
		};
		_i = _i+1;
	} forEach _zones;
};

_pos = _startPos;
_code = "";
if (_myZoneI != -1) then {
	//hint "in marker zone";
	_myZone = _zones select _myZoneI;
	_code = "_res = [_checkPos,_myZone] call CBA_fnc_inArea; _res"
}else{
	//hint "in _safeDistance zone";
	createMarkerLocal [_myZone,_pos];
	_myZone setMarkerShapeLocal "Ellipse";
	_myZone setMarkerSizeLocal [_safeDistance,_safeDistance];
	_code = "_dist = _checkPos distance _startPos; (_dist < _safeDistance)"	
};
_code = compile _code;
if (_colorMarker) then {
	_myZone setMarkerBrushLocal "SOLID";
	_myZone setMarkerColorLocal "ColorGreen";
	_myZone setMarkerAlphaLocal 0.2;
};

waitUntil {
	sleep _timeout;
	_checkPos = getPosATL vehicle _unit;
	_inZone = call _code;
	if (_inZone) then {
		_pos = _checkPos;		
	}else{
		hint "Stop!";
		(vehicle _unit) setPosATL _pos;
		(vehicle _unit) setVelocity [0, 0, 0];
	};
	call _cond
};
_myZone setMarkerAlphaLocal 0;
true